package com.hrbxlh.virtualization.controller;

import com.github.pagehelper.PageInfo;
import com.hrbxlh.virtualization.param.ImageCoverParam;
import com.hrbxlh.virtualization.param.ImageCreateParam;
import com.hrbxlh.virtualization.result.Result;
import com.hrbxlh.virtualization.service.ImageService;
import com.hrbxlh.virtualization.util.ObjectUtil;
import com.hrbxlh.virtualization.util.SessionUtil;
import com.hrbxlh.virtualization.vo.DesktopVO;
import com.hrbxlh.virtualization.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    /**
     * 查询镜像列表
     *
     * @param isInitial 是否为预置镜像
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List<ImageVO>> listImages(@RequestParam(required = false) Boolean isInitial) {
        List<ImageVO> imageVOList = imageService.listImages(isInitial, SessionUtil.getStuorteaId());
        return Result.success(imageVOList);
    }

    /**
     * 分页查询镜像列表
     *
     * @param pageNum  页数
     * @param pageSize 每页大小
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result<PageInfo<ImageVO>> listImagesByPage(@RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize) {

        PageInfo<ImageVO> page = imageService.listImagesByPage(pageNum, pageSize, SessionUtil.getStuorteaId());
        return Result.success(page);
    }

    /**
     * 查看镜像
     *
     * @param imageId 镜像Id
     */
    @RequestMapping(value = "/desktop", method = RequestMethod.GET)
    public Result<DesktopVO> getDesktop(Long imageId) {
        DesktopVO desktopVO = imageService.getDesktop(SessionUtil.getStuorteaId(), imageId);
        return Result.success(desktopVO);
    }

    /**
     * 保存镜像
     *
     * @return 镜像Id
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result<Map<String, Object>> createImage(@RequestBody @Valid ImageCreateParam imageCreateParam) {
        Long imageId = imageService.createImage(SessionUtil.getStuorteaId(), imageCreateParam);
        return Result.success(ObjectUtil.build().addObject("imageId", imageId).map());
    }

    /**
     * 重新选择镜像
     *
     * @param containerId 容器Id
     * @param imageId     镜像Id
     */
    @RequestMapping(value = "/repick", method = RequestMethod.GET)
    public Result<DesktopVO> repickImage(Long containerId, Long imageId) {
        DesktopVO desktopVO = imageService.repickImage(SessionUtil.getStuorteaId(), containerId, imageId);
        return Result.success(desktopVO);
    }

    /**
     * 共享镜像
     *
     * @param imageId 镜像Id
     */
    @RequestMapping(value = "/share", method = RequestMethod.GET)
    public Result shareImage(Long imageId) {
        imageService.shareImage(SessionUtil.getStuorteaId(), imageId);
        return Result.success();
    }

    /**
     * 取消共享镜像
     *
     * @param imageId 镜像Id
     */
    @RequestMapping(value = "/unshare", method = RequestMethod.GET)
    public Result cancelShareImage(Long imageId) {
        imageService.unshareImage(SessionUtil.getStuorteaId(), imageId);
        return Result.success();
    }

    /**
     * 删除镜像
     *
     * @param id       镜像Id
     * @param isForced 是否强制删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result deleteImage(Long id, @RequestParam(required = false, defaultValue = "false") Boolean isForced) {
        imageService.deleteImage(id, isForced, SessionUtil.getStuorteaId());
        return Result.success();
    }

    /**
     * 覆盖镜像
     *
     * @return 镜像Id
     */
    @RequestMapping(value = "/cover", method = RequestMethod.POST)
    public Result<Map<String, Object>> coverImage(@RequestBody @Valid ImageCoverParam imageCoverParam) {
        Long imageId = imageService.coverImage(SessionUtil.getStuorteaId(), imageCoverParam);
        return Result.success(ObjectUtil.build().addObject("imageId", imageId).map());
    }
}
